//
//  MSAwardListVC.m
//  MSMain
//
//  Created by Dgh Hv on 2018/9/19.
//  Copyright © 2018年 土豆. All rights reserved.
//

#import "MSAwardListVC.h"
#import "MSRewardTVC.h"
#import "MSInviteViewModel.h"
#import "BMHPromptView.h"

static NSString * MSRewardTVCID = @"MSRewardTVCID";
@interface MSAwardListVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}
@property (nonatomic, strong)MSInviteViewModel * viewModel;
@property (nonatomic, strong)NSMutableArray * dataArray;
@property (nonatomic, strong)BMHPromptView * promptView;

@end

@implementation MSAwardListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHidenNaviBar = YES;
    page = 1;
    [self getData];
    [self setUI];
    // Do any additional setup after loading the view.
}

-(void)setUI
{
    self.view.backgroundColor = KViewBackguroud;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - kNavBarHeight - kStatusBarHeight - kBottonSafaArea);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    CGRect frame=CGRectMake(0, 0, 0, 0.01f);
    self.tableView.tableHeaderView=[[UIView alloc]initWithFrame:frame];
    [self.tableView registerNib:[UINib nibWithNibName:@"MSRewardTVC" bundle:nil] forCellReuseIdentifier:MSRewardTVCID];
    [self.view addSubview:self.tableView];
}

-(void)getData
{
    kWeakSelf(self);
    [self.viewModel getInviteRewardListParams:@{@"pageIndex":@(page),@"pageSize":@"10"} Complete:^(NSArray *data) {
        [weakself.dataArray addObjectsFromArray:data];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        [weakself.tableView.mj_header endRefreshing];
        
        if(!ValidArray(weakself.dataArray))
        {
            self.promptView.hidden = NO;
        }else
        {
            [self.promptView removeFromSuperview];
            self.promptView = nil;
        }
    }];
}




#pragma mark ========上下拉刷新
-(void)headerRereshing{
    page = 1;
    [self.dataArray removeAllObjects];
    [self getData];
    
}

-(void)footerRereshing{
    page ++;
    [self getData];
}


#pragma mark ---tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MSRewardTVC * cell = [tableView dequeueReusableCellWithIdentifier:MSRewardTVCID];
    if(indexPath.row < self.dataArray.count)
    {
        [cell getInfoFromModel:self.dataArray[indexPath.row]];
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getters & setters

-(MSInviteViewModel *)viewModel
{
    if(_viewModel == nil)
    {
        _viewModel = [[MSInviteViewModel alloc]init];
    }
    return _viewModel;
}

-(NSMutableArray *)dataArray
{
    if(_dataArray == nil)
    {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}


- (BMHPromptView *)promptView {
    if (_promptView == nil) {
        _promptView = [[BMHPromptView alloc] initWithImage:[UIImage imageNamed:@"pic_nothing_n"]];
        [_promptView.reTryButton setTitle:@"还没有内容呢～" forState:UIControlStateNormal];
        [self.view addSubview:_promptView];
        [_promptView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(200, 215));
            make.center.equalTo(self.view);
        }];
    }
    return _promptView;
}


@end
